package org.hxzon.workeasy.pages.user;

import org.apache.tapestry5.EventConstants;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.hxzon.workeasy.model.EasyUzer;
import org.hxzon.workeasy.pages.base.MyBasePage;
import org.hxzon.workeasy.permissions.UserCDUPermissions;
import org.hxzon.workeasy.service.EasyUserService;
import org.hxzon.workeasy.util.HlogConstants;

public class CreateUser extends MyBasePage {

    @Inject
    private EasyUserService userSer;
    @Property
    private EasyUzer user;
    @InjectPage
    private ListUser listUser;

    @OnEvent(value = EventConstants.ACTIVATE)
    Object init() {
        new UserCDUPermissions().check(null, this.getVisitor());
        user = new EasyUzer();
        return null;
    }

    @OnEvent(component = "add")
    Object add() {
        new UserCDUPermissions().check(null, this.getVisitor());
        user.md5password();
        user.setAdminGrade(EasyUzer.Power_Normal);
        userSer.save(user);
        hlog(HlogConstants.addUser, user);
        return listUser;
    }
}
